package com.zxm.web.controller.txcloud.ocr;

import com.zxm.common.core.domain.AjaxResult;
import com.zxm.common.core.domain.TxyFactory;
import com.zxm.common.core.domain.entity.TxyGeneralOcr;
import com.zxm.common.utils.StringUtils;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author silence.Z
 * @Date 2022/6/9 16:17
 * @PackageName:com.zxm.web.controller.txcloud.ocr
 * @ClassName: CardController
 * @Description: 通用文字识别
 * @Version 1.0
 */
@RestController
@RequestMapping("txcloud/ocr/general")
public class GeneralController {
	private static final Logger log = LoggerFactory.getLogger(GeneralController.class);


	@PreAuthorize("@ss.hasPermi('txcloud:ocr:general:list')")
	@PostMapping("textDetect")
	/**
	 * 快速文本检测(检查图片中是否包含文字)
	 */
	public AjaxResult textDetect(@Validated @RequestBody TxyGeneralOcr generalOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		TextDetectRequest req = new TextDetectRequest();
		if (StringUtils.isNotEmpty(generalOcr.getImageBase64())){
			req.setImageBase64(generalOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(generalOcr.getImageUrl())){
			req.setImageUrl(generalOcr.getImageUrl());
		}
		TextDetectResponse resp = client.TextDetect(req);
		// 输出json格式的字符串回包
		log.info(TextDetectResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}
	@PreAuthorize("@ss.hasPermi('txcloud:ocr:general:list')")
	@PostMapping("generalBasicOCR")
	/**
	 * 通用印刷体识别
	 */
	public AjaxResult generalBasicOCR(@Validated @RequestBody TxyGeneralOcr generalOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
		if (StringUtils.isNotEmpty(generalOcr.getImageBase64())){
			req.setImageBase64(generalOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(generalOcr.getImageUrl())){
			req.setImageUrl(generalOcr.getImageUrl());
		}
		if (StringUtils.isNotEmpty(generalOcr.getIsPdf().toString())){
			req.setIsPdf(generalOcr.getIsPdf());
		}
		if (StringUtils.isNotEmpty(generalOcr.getLanguageType())){
			req.setLanguageType(generalOcr.getLanguageType());
		}
		if (StringUtils.isNotEmpty(generalOcr.getPdfPageNumber().toString())){
			req.setPdfPageNumber(Long.valueOf(generalOcr.getPdfPageNumber()));
		}
		GeneralBasicOCRResponse resp = client.GeneralBasicOCR(req);
		log.info(GeneralBasicOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}
	@PreAuthorize("@ss.hasPermi('txcloud:ocr:general:list')")
	@PostMapping("generalAccurateOCR")
	/**
	 * 通用印刷体识别（高精度版）
	 */
	public AjaxResult generalAccurateOCR(@Validated @RequestBody TxyGeneralOcr generalOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest();
		if (StringUtils.isNotEmpty(generalOcr.getImageBase64())){
			req.setImageBase64(generalOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(generalOcr.getImageUrl())){
			req.setImageUrl(generalOcr.getImageUrl());
		}
		if (StringUtils.isNotEmpty(generalOcr.getIsPdf().toString())){
			req.setIsPdf(generalOcr.getIsPdf());
		}
		if (StringUtils.isNotEmpty(generalOcr.getPdfPageNumber().toString())){
			req.setPdfPageNumber(Long.valueOf(generalOcr.getPdfPageNumber()));
		}
		GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req);
		// 输出json格式的字符串回包
		log.info(GeneralAccurateOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}

	@PreAuthorize("@ss.hasPermi('txcloud:ocr:general:list')")
	@PostMapping("generalHandwritingOCR")
	/**
	 * 通用手写体识别
	 */
	public AjaxResult generalHandwritingOCR(@Validated @RequestBody TxyGeneralOcr generalOcr) throws TencentCloudSDKException {
		OcrClient client = TxyFactory.createOcr();
		GeneralHandwritingOCRRequest req = new GeneralHandwritingOCRRequest();
		if (StringUtils.isNotEmpty(generalOcr.getImageBase64())){
			req.setImageBase64(generalOcr.getImageBase64());
		}
		if (StringUtils.isNotEmpty(generalOcr.getImageUrl())){
			req.setImageUrl(generalOcr.getImageUrl());
		}
		GeneralHandwritingOCRResponse resp = client.GeneralHandwritingOCR(req);
		// 输出json格式的字符串回包
		log.info(GeneralHandwritingOCRResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}
}
